//
//  GameCenterViewController.m
//  GameBaby
//
//  Created by keyZhang on 13-12-31.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//

#import "GameCenterViewController.h"
#import "GiftButton.h"
#import "DataService.h"
#import "ZXCellModel.h"
#import "CommentCellModel.h"
#import "GiftDetailViewController.h"
#import "AppDelegate.h"

@interface GameCenterViewController ()
{
    UIScrollView *scrView;
    UILabel *gNameLabel;
    UILabel *gTypeLabel;
    UILabel *gStatus;
    UILabel *gVersion;
    UILabel *gSize;
    UILabel *gTime;
    UIImageView *headImg;
    UILabel *gIntr;
    UILabel *gameIntro;
    UILabel *gImgTitle;
    UIScrollView *imgScrollView;
    
//    UIButton *downloadBtn;
//    UIButton *shareBtn;
    UIView *balckWindow;
    
}
@property (nonatomic, strong) NSDictionary *xqDic;
@property (nonatomic, copy) NSArray *glImgArray;

@end

@implementation GameCenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"游戏专区";
        self.navigationItem.titleView = t;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    __weak GameCenterViewController *this = self;
    self.zxTableView.finishBlock = ^{
        [this _loadZXMoreData];
    };
    self.glTableView.finishBlock = ^{
        [this _loadGLMoreData];
    };
    self.cmtTableView.finishBlock = ^{
        [this _loadCommentMoreData];
    };
    
    self.zxTableView.rowHeight = 80.0;
    self.glTableView.rowHeight = 80;
    self.cmtTableView.rowHeight = 80;
    
    _gLdataList = [NSMutableArray array];
    _zXdataList = [NSMutableArray array];
    _pLdataList = [NSMutableArray array];
    
    self.glTableView.refreshDelegate = self;
    self.zxTableView.refreshDelegate = self;
    self.cmtTableView.refreshDelegate = self;

    [self loadXQdata];
    
    [self loadXqView];
    
    UISwipeGestureRecognizer *leftswipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    leftswipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftswipeRecognizer];
    
    UISwipeGestureRecognizer *rightswipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    rightswipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightswipeRecognizer];
    
    self.commentTexfield.height = 36;
    
    self.commentBottomView.top = self.changeVIew.bottom - 2;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    
    UITapGestureRecognizer *tGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tGRtapAction:)];
    [self.cmtTableView addGestureRecognizer:tGR];
}

/**
 *  点击键盘消失
 *
 * 
 */
- (void)tGRtapAction:(UITapGestureRecognizer *)tap
{
    if (self.commentTexfield.editing) {
        [self.commentTexfield resignFirstResponder];
        self.commentBottomView.top = self.changeVIew.bottom - 2;
    }
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************


- (void)initHeaderView
{
    int imgNum = self.glImgArray.count;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(1, 85, 318, 150)];
    _scrollView.contentSize = CGSizeMake(320 * imgNum, 150);
    _scrollView.bounces = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    self.glTableView.tableHeaderView = _scrollView;
    
    for (int i = 0; i < imgNum; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 150)];
        imageView.tag = 50+i;
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(glTapAction:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tapRecognizer];
        [imageView setImageWithURL:[NSURL URLWithString:self.glImgArray[i]]];
        [_scrollView addSubview:imageView];
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 133, 320, 20)];
    pageControl.enabled = NO;
    pageControl.tag = 100;
    pageControl.numberOfPages = imgNum;
    if (imgNum == 1) {
        pageControl.hidden = YES;
    }
    [self.glTableView addSubview:pageControl];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void)glTapAction:(UITapGestureRecognizer *)tap
{
    UIScrollView *bigScrollView;

    int imgNum = self.glImgArray.count;
    bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    bigScrollView.contentSize = CGSizeMake(320 , 150 * imgNum);
    bigScrollView.bounces = YES;
    bigScrollView.pagingEnabled = YES;
    bigScrollView.showsHorizontalScrollIndicator = NO;
    bigScrollView.showsVerticalScrollIndicator = NO;
    bigScrollView.delegate = self;

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigGlTapAction:)];
    [bigScrollView addGestureRecognizer:tapRecognizer];

    for (int i = 0; i < imgNum; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 150 * i, 320, 150)];
        imageView.userInteractionEnabled = YES;
        [imageView setImageWithURL:[NSURL URLWithString:self.glImgArray[i]]];
        [bigScrollView addSubview:imageView];
    }
    [bigScrollView setContentOffset:CGPointMake(0, 150 * (tap.view.tag - 50)) animated:NO];

    balckWindow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];

    balckWindow.backgroundColor = [UIColor blackColor];
    [balckWindow addSubview:bigScrollView];
    [[(AppDelegate *)[UIApplication sharedApplication].delegate window] addSubview:balckWindow];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.35];
    CGAffineTransform tarnsForm = CGAffineTransformRotate(bigScrollView.transform, -M_PI_2);
//    bigScrollView.transform = CGAffineTransformScale(tarnsForm, kScreenHeight/(float)kScreenWidth, kScreenWidth/150);
    bigScrollView.transform = CGAffineTransformScale(tarnsForm, 480/(float)kScreenWidth, kScreenWidth/150);

    bigScrollView.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
    [UIView commitAnimations];
}

- (void)bigGlTapAction:(UITapGestureRecognizer *)tap
{
    [tap.view removeFromSuperview];
    [balckWindow removeFromSuperview];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

}

static int count = 0;
- (void)timerAction:(NSTimer *)timer
{
    int imgNum = self.glImgArray.count;
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:100];

    count = pageControl.currentPage;
    pageControl.currentPage++;
    count ++;
    if (count == imgNum) {
        pageControl.currentPage = 0;
        [(UIScrollView *)self.glTableView.tableHeaderView setContentOffset:CGPointMake(0, 0) animated:NO];
    }else
    {
        [(UIScrollView *)self.glTableView.tableHeaderView setContentOffset:CGPointMake(320 * count , 0) animated:YES];
    }
    
}

#pragma mark - UIScrollViewDelegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:100];
    pageControl.currentPage = scrollView.contentOffset.x/320;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.timer invalidate];
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************




- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    [self.commentTexfield resignFirstResponder];
    self.commentBottomView.top = self.changeVIew.bottom - 2;
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSInteger tag_value = [[[self.changeVIew subviews] lastObject] tag];
        if (tag_value <= 30) {
            UIView *chooseView = [self.changeVIew viewWithTag:tag_value + 10];
            UIView *backView = [self.changeVIew viewWithTag:tag_value];

            //            [UIView animateWithDuration:0.35 animations:^{
            //                [self.changeView bringSubviewToFront:chooseView];
            //                self.arrowImgView.left = self.arrowImgView.left + 70;
            ////            }];
            chooseView.left = kScreenWidth;

            
            UIViewAnimationTransition transition = UIViewAnimationTransitionNone;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationTransition:transition forView:self.changeVIew cache:YES];
            //
            backView.right = 0;
            chooseView.left = 0;
            [self.changeVIew bringSubviewToFront:chooseView];
            self.chooseImgView.left = self.chooseImgView.left + 80;
            
            [UIView commitAnimations];
            
            //            [UIView animateWithDuration:0.7 delay:0 options:0 animations:^(void){
            //                [self.changeView bringSubviewToFront:chooseView];
            //                self.arrowImgView.left = self.arrowImgView.left + 70;
            //            } completion:^(BOOL finished)
            //             {
            //
            //             }];
            if (tag_value == 30) {
                self.bottomView.hidden = YES;
                self.commentBottomView.hidden = NO;
            }
        }
    }
    
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSInteger tag_value = [[[self.changeVIew subviews] lastObject] tag];
        if (tag_value > 10) {
            UIView *chooseView = [self.changeVIew viewWithTag:tag_value - 10];
            UIView *backView = [self.changeVIew viewWithTag:tag_value];

            //            [UIView animateWithDuration:0.35 animations:^{
            //                [self.changeView bringSubviewToFront:chooseView];
            //                self.arrowImgView.left = self.arrowImgView.left - 70;
            //            }];
            chooseView.right = 0;

            
            UIViewAnimationTransition transition = UIViewAnimationTransitionNone;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationTransition:transition forView:self.changeVIew cache:YES];
            //
            chooseView.left = 0;
            backView.left = kScreenWidth;
            [self.changeVIew bringSubviewToFront:chooseView];
            self.chooseImgView.left = self.chooseImgView.left - 80;
            
            [UIView commitAnimations];
            
            self.bottomView.hidden = NO;
            self.commentBottomView.hidden = YES;

        }
    }
}


- (void)loadXqView
{
    scrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 88)];
    scrView.backgroundColor = [UIColor whiteColor];
    scrView.tag = 10;
    
//    头像
    headImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    [scrView addSubview:headImg];
    
//    名称
    gNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 150, 20)];
    gNameLabel.font = [UIFont systemFontOfSize:16];
    [scrView addSubview:gNameLabel];

//    类型
    gTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 32, 150, 15)];
    gTypeLabel.font = [UIFont systemFontOfSize:13];
    gTypeLabel.textColor = [UIColor darkGrayColor];
    [scrView addSubview:gTypeLabel];

//    状态
    gStatus = [[UILabel alloc] initWithFrame:CGRectMake(100, 52, 120, 15)];
    gStatus.font = [UIFont systemFontOfSize:13];
    gStatus.textColor = [UIColor darkGrayColor];
    [scrView addSubview:gStatus];

//    版本
    gVersion = [[UILabel alloc] initWithFrame:CGRectMake(188, 52, 120, 15)];
    gVersion.font = [UIFont systemFontOfSize:13];
    gVersion.textColor = [UIColor darkGrayColor];
    [scrView addSubview:gVersion];

//    大小
    gSize = [[UILabel alloc] initWithFrame:CGRectMake(100, 73, 100, 15)];
    gSize.font = [UIFont systemFontOfSize:13];
    gSize.textColor = [UIColor darkGrayColor];
    [scrView addSubview:gSize];

//    更新时间
    gTime = [[UILabel alloc] initWithFrame:CGRectMake(188, 73, 140, 15)];
    gTime.font = [UIFont systemFontOfSize:13];
    gTime.textColor = [UIColor darkGrayColor];
    [scrView addSubview:gTime];

//    游戏介绍标题
    gIntr = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 60, 21)];
    gIntr.textColor = [UIColor darkGrayColor];
    gIntr.font = [UIFont systemFontOfSize:12];
    [scrView addSubview:gIntr];
    
//    游戏介绍
    gameIntro = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 100)];
    gameIntro.backgroundColor = [UIColor clearColor];
    gameIntro.numberOfLines = 4;
    gameIntro.textColor = [UIColor lightGrayColor];
    gameIntro.font = [UIFont systemFontOfSize:12];
    gameIntro.userInteractionEnabled = YES;
    [scrView addSubview:gameIntro];
//    游戏截图标题
    gImgTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 60, 21)];
    gImgTitle.textColor = [UIColor darkGrayColor];
    gImgTitle.font = [UIFont systemFontOfSize:12];
    [scrView addSubview:gImgTitle];
//    游戏截图内容
    imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    imgScrollView.showsHorizontalScrollIndicator = NO;
    imgScrollView.pagingEnabled = YES;

    [scrView addSubview:imgScrollView];
    
    [self.changeVIew addSubview:scrView];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSString *textStr = [(UILabel *)tap.view text];
    UIFont *font = [UIFont systemFontOfSize:12];
    CGSize size = CGSizeMake(kScreenWidth - 20, 2000);
    CGSize labelsize = [textStr sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    if (gameIntro.numberOfLines == 4) {
        gameIntro.numberOfLines = 0;
        gameIntro.height = labelsize.height + 40;
        
    }else
    {
        gameIntro.numberOfLines = 4;
        gameIntro.height = 100;
    }
    
    gImgTitle.top = gameIntro.bottom - 10;
    
    imgScrollView.top = gImgTitle.bottom;
    
//    downloadBtn.top = imgScrollView.bottom +5;
//    shareBtn.top = downloadBtn.top;
    
    scrView.contentSize = CGSizeMake(kScreenWidth, imgScrollView.bottom + 64);
    
}

- (void)setXqDic:(NSDictionary *)xqDic
{
    _xqDic = xqDic;
    
    [headImg setImageWithURL:[NSURL URLWithString:[xqDic objectForKey:@"litpic"]]];
    gNameLabel.text = [xqDic objectForKey:@"gamename"];
    gTypeLabel.text = [NSString stringWithFormat:@"类型:%@",[xqDic objectForKey:@"gametype"]];
    gStatus.text = [NSString stringWithFormat:@"状态:%@",[xqDic objectForKey:@"status"]];
    gVersion.text = [NSString stringWithFormat:@"版本:%@",[xqDic objectForKey:@"version"]];
    gSize.text = [NSString stringWithFormat:@"大小:%@",[xqDic objectForKey:@"softsize"]];
    gTime.text = [NSString stringWithFormat:@"更新时间:%@",[xqDic objectForKey:@"status_time"]];
    
    int acatNum = [[xqDic objectForKey:@"acat"] intValue];
    int spreeNum = [[xqDic objectForKey:@"spree"] intValue];
    
    if (acatNum ==0 && spreeNum == 0) {
        
    }

    NSDictionary *relatedDic = [xqDic objectForKey:@"related"];
    
    NSArray *acatArray;
    NSArray *spreeArray;
    
    if (relatedDic.count != 0) {
        acatArray = [relatedDic objectForKey:@"acat"];
        spreeArray = [relatedDic objectForKey:@"spree"];
    }
    
    if (acatNum != 0)
    {
        for (int i = 0; i < acatNum; i++) {
            
            NSDictionary *acatDic = acatArray[i];
            
            GiftButton *giftBtn = [GiftButton buttonWithType:UIButtonTypeCustom];
            
            giftBtn.frame = CGRectMake(10, 0, 0, 0);
            giftBtn.top = gTime.bottom + 20 + i*42;
            giftBtn.isRedBg = [[acatDic objectForKey:@"type"] intValue] == 2 ? YES : NO;
            giftBtn.hasTao = [[acatDic objectForKey:@"tao"] intValue] == 1 ? YES : NO;
            giftBtn.hasLing = [[acatDic objectForKey:@"lika"] intValue] == 1 ? YES : NO;
            giftBtn.btnTitle = [acatDic objectForKey:@"title"];
            giftBtn.giftId = [acatDic objectForKey:@"id"];
            
            [giftBtn addTarget:self action:@selector(giftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [scrView addSubview:giftBtn];
        }
        
    }
    
    if (spreeNum != 0) {
        for (int i = 0; i < spreeNum; i ++) {
            
            NSDictionary *spreeDic = spreeArray[i];
            
            GiftButton *giftBtn = [GiftButton buttonWithType:UIButtonTypeCustom];
            giftBtn.frame = CGRectMake(10, 0, 0, 0);
            giftBtn.top = gTime.bottom + 20 + (acatNum+ i)*42;
            giftBtn.isRedBg = [[spreeDic objectForKey:@"type"] intValue] == 2 ? YES : NO;
            giftBtn.hasTao = [[spreeDic objectForKey:@"tao"] intValue] == 1 ? YES : NO;
            giftBtn.hasLing = [[spreeDic objectForKey:@"lika"] intValue] == 1 ? YES : NO;
            giftBtn.btnTitle = [spreeDic objectForKey:@"title"];
            giftBtn.giftId = [spreeDic objectForKey:@"id"];
            
            
            [giftBtn addTarget:self action:@selector(giftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [scrView addSubview:giftBtn];
        }
    }
    
    gIntr.text = @"游戏介绍";
    gIntr.top = gTime.bottom + 20 + (acatNum + spreeNum)*42;
    gameIntro.text = [xqDic objectForKey:@"introduction"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [gameIntro addGestureRecognizer:tap];
    gameIntro.top = gIntr.bottom-20;
    gImgTitle.top = gameIntro.bottom - 10;
    gImgTitle.text = @"游戏截图";

    imgScrollView.top = gImgTitle.bottom;
    
//游戏截图设置
    NSArray * gameimgArr = [xqDic objectForKey:@"images"];
    imgScrollView.contentSize = CGSizeMake(320 * gameimgArr.count, 150);
    for (int i = 0; i < gameimgArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i + 20, 5, 280, 140)];
        [imageView setImageWithURL:[NSURL URLWithString:gameimgArr[i]]];
        [imgScrollView addSubview:imageView];
    }

    scrView.contentSize = CGSizeMake(kScreenWidth, imgScrollView.bottom + 64);
    
    
    NSString *urlStr = [self.xqDic objectForKey:@"download"];
    if (![urlStr isEqualToString:@""]) {
        [self.downLoadBtn setTitle:@"下载" forState:UIControlStateNormal];
        self.downLoadBtn.userInteractionEnabled = YES;
    }else
    {
        [self.downLoadBtn setBackgroundImage:[UIImage imageNamed:@"nx2_14H"] forState:UIControlStateNormal];
        self.downLoadBtn.userInteractionEnabled = NO;
    }
//    关注按钮的设置
    if ((BOOL)[[self.xqDic objectForKey:@"is_attention"]intValue]) {
        [self.attentionBtn setTitle:@"取消关注" forState:UIControlStateNormal];
    }else
    {
        [self.attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
    }
}

- (void)giftBtnClick:(UIButton *)btn
{
    GiftDetailViewController *gditailVC = [[GiftDetailViewController alloc] init];
    gditailVC.detailID = [(GiftButton *)btn giftId];
    [self.navigationController pushViewController:gditailVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)detailBtnClick:(UIButton *)sender {
    self.bottomView.hidden = NO;
    self.commentBottomView.hidden = YES;
    [self.commentTexfield resignFirstResponder];
    self.chooseImgView.left = sender.left;
    UIView *backView = [self.changeVIew viewWithTag:10];
    [self.changeVIew bringSubviewToFront:backView];
    backView.left = 0;}

- (IBAction)zxBtnClick:(UIButton *)sender {
    self.bottomView.hidden = NO;
    self.commentBottomView.hidden = YES;
    [self.commentTexfield resignFirstResponder];
    self.chooseImgView.left = sender.left;
    UIView *backView = [self.changeVIew viewWithTag:20];
    [self.changeVIew bringSubviewToFront:backView];
    backView.left = 0;}

- (IBAction)glBtnClick:(UIButton *)sender {
    self.bottomView.hidden = NO;
    self.commentBottomView.hidden = YES;
    [self.commentTexfield resignFirstResponder];
    self.chooseImgView.left = sender.left;
    UIView *backView = [self.changeVIew viewWithTag:30];
    [self.changeVIew bringSubviewToFront:backView];
    backView.left = 0;
}

- (IBAction)commentBtnClick:(UIButton *)sender {
    self.bottomView.hidden = YES;
    self.commentBottomView.hidden = NO;
    [self.commentTexfield resignFirstResponder];
    self.commentBottomView.top = self.changeVIew.bottom - 2;
    self.chooseImgView.left = sender.left;
    UIView *backView = [self.changeVIew viewWithTag:40];
    [self.changeVIew bringSubviewToFront:backView];
    backView.left = 0;
}

/*-------------------------------详情---------------------------------------*/
- (void)loadXQdata
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",nil];
    __weak GameCenterViewController *this = self;

    [DataService requestWithURL:self.gameXqUrl
                         params:dic
                     httpMethod:@"POST"
                    finishBlock:^(id result) {
                        [this loadXQDataFinished:result];
                    }];
}
- (void)loadXQDataFinished:(NSDictionary *)result
{
    NSLog(@"result:%@",result);
    self.xqDic = result;
    
    [self loadZXData];
    
    [self loadGLData];
    
    [self loadCommentData];
}



/*--------------------------资讯-------------------------------------------*/

- (void)loadZXData
{
    NSLog(@"%@",[self.xqDic objectForKey:@"gid"]);
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/html/works/news/%@/%d",[self.xqDic objectForKey:@"gid"],self.zXdataList.count/10+1];
    __weak GameCenterViewController *this = self;
    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadZXDataFinished:result];
                    }];
}

- (void)_loadZXMoreData
{
    _zXdataList = [NSMutableArray array];
    [self loadZXData];
}

- (void)loadZXDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];
    
    if (array.count < 10) {
        self.zxTableView.isMore = NO;
        for (NSDictionary *dic in array) {
            ZXCellModel *model = [[ZXCellModel alloc] initContentWith:dic];
            [self.zXdataList addObject:model];
        }
        self.zxTableView.dataList = self.zXdataList;
        [self.zxTableView reloadData];
        [self.zxTableView doneLoadingTableViewData];
    }else
    {
        for (NSDictionary *dic in array) {
            ZXCellModel *model = [[ZXCellModel alloc] initContentWith:dic];
            [self.zXdataList addObject:model];
        }
        self.zxTableView.dataList = self.zXdataList;
        [self.zxTableView reloadData];
        [self.zxTableView doneLoadingTableViewData];
        if (self.zXdataList.count > 99) {
            self.zxTableView.isMore = NO;
        }else
        {
            self.zxTableView.isMore = YES;
        }
    }
    
}

/*--------------------------攻略-----------------------------------------*/

- (void)loadGLData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/html/works/skill/%@/%d",[self.xqDic objectForKey:@"gid"],self.gLdataList.count/10+1];
    __weak GameCenterViewController *this = self;

    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadGLDataFinished:result];
                    }];
}

- (void)_loadGLMoreData
{
    _gLdataList = [NSMutableArray array];
    [self loadGLData];
}

- (void)loadGLDataFinished:(NSDictionary *)result
{
    NSLog(@"%@",result);
    NSArray *glImgArr = [result objectForKey:@"litpic"];
    self.glImgArray = glImgArr;
    if (glImgArr) {
        if (!self.glTableView.tableHeaderView) {
            [self initHeaderView];
        }
    }
    NSArray *array = [result objectForKey:@"list"];
    
    if (array.count < 10) {
        self.glTableView.isMore = NO;
        for (NSDictionary *dic in array) {
            ZXCellModel *model = [[ZXCellModel alloc] initContentWith:dic];
            [self.gLdataList addObject:model];
        }
        self.glTableView.dataList = self.gLdataList;
        [self.glTableView reloadData];
        [self.glTableView doneLoadingTableViewData];
    }else
    {
        for (NSDictionary *dic in array) {
            ZXCellModel *model = [[ZXCellModel alloc] initContentWith:dic];
            [self.gLdataList addObject:model];
        }
        self.glTableView.dataList = self.gLdataList;
        [self.glTableView reloadData];
        [self.glTableView doneLoadingTableViewData];
        if (self.gLdataList.count > 99) {
            self.glTableView.isMore = NO;
        }else
        {
            self.glTableView.isMore = YES;
        }
    }
}

/*--------------------------评论-----------------------------------------*/
- (void)loadCommentData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/html/feedback/%@/%d",[self.xqDic objectForKey:@"identity"],self.pLdataList.count/10+1];
    __weak GameCenterViewController *this = self;

    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadCommentDataFinished:result];
                    }];
}

- (void)_loadCommentMoreData
{
    _pLdataList = [NSMutableArray array];
    [self loadCommentData];

}

- (void)loadCommentDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];
    
    if (array.count < 10) {
        self.cmtTableView.isMore = NO;
        for (NSDictionary *dic in array) {
            CommentCellModel *model = [[CommentCellModel alloc] initContentWith:dic];
            [self.pLdataList addObject:model];
        }
        self.cmtTableView.dataList = self.pLdataList;
        [self.cmtTableView reloadData];
        [self.cmtTableView doneLoadingTableViewData];
    }else
    {
        for (NSDictionary *dic in array) {
            CommentCellModel *model = [[CommentCellModel alloc] initContentWith:dic];
            [self.pLdataList addObject:model];
        }
        self.cmtTableView.dataList = self.pLdataList;
        [self.cmtTableView reloadData];
        [self.cmtTableView doneLoadingTableViewData];
        if (self.pLdataList.count > 99) {
            self.cmtTableView.isMore = NO;
        }else
        {
            self.cmtTableView.isMore = YES;
        }
    }
    
}



#pragma mark - BaseTableView delegate
//上拉加载的协议方法
- (void)pullUp:(BaseTableView *)tableView {
    if (tableView.tag == 20) {
        NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
        if (![str isEqualToString:@"加载更多数据"]) {
            return;
        }else
        {
            [self loadZXData];
        }

    }
    if (tableView.tag == 30) {
        NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
        if (![str isEqualToString:@"加载更多数据"]) {
            return;
        }else
        {
            [self loadGLData];
        }
    }
    if (tableView.tag == 40) {
        NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
        if (![str isEqualToString:@"加载更多数据"]) {
            return;
        }else
        {
            [self loadCommentData];
        }
    }
}




/**
 *
 *
 *  @param sender 游戏专区下载和关注
 */
- (IBAction)downLoadAction:(UIButton *)sender {
    
    
    NSString *urlStr = [self.xqDic objectForKey:@"download"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",urlStr]]];
}

- (IBAction)attentionAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"关注"]) {
        NSString *url = [NSString stringWithFormat:@"http://m.a.ewoka.com/apply/attentions"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",[self.xqDic objectForKey:@"gid"],@"gid", nil];
        [DataService requestWithURL:url
                             params:dic
                         httpMethod:@"POST"
                        finishBlock:^(id result) {
                            [sender setTitle:@"取消关注" forState:UIControlStateNormal];
                            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"提示",nil),nil] message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil];
                            [alertview show];
                        }];
    }else if ([sender.titleLabel.text isEqualToString:@"取消关注"])
    {
        NSString *url = [NSString stringWithFormat:@"http://m.a.ewoka.com/apply/un_attentions"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",[self.xqDic objectForKey:@"gid"],@"gid", nil];
        [DataService requestWithURL:url
                             params:dic
                         httpMethod:@"POST"
                        finishBlock:^(id result) {
                            [sender setTitle:@"关注" forState:UIControlStateNormal];
                            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"提示",nil),nil] message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil];
                            [alertview show];
                        }];
    }
}

/**
 *  发送评论
 *
 */
- (IBAction)sendCommentBtnClick:(UIButton *)sender {
    
    [self.commentTexfield resignFirstResponder];
    self.commentBottomView.top = self.changeVIew.bottom - 2;
    NSString *url = [NSString stringWithFormat:@"http://m.a.ewoka.com/html/feedback_install/sendCommentContant"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",[self.xqDic objectForKey:@"identity"],@"identity",self.commentTexfield.text,@"content",nil];
    __weak GameCenterViewController *this = self;

    [DataService requestWithURL:url
                         params:dic
                     httpMethod:@"POST"
                    finishBlock:^(id result) {
                        if ([[result objectForKey:@"msg"] intValue] == 0) {
                            this.commentTexfield.text = @"";
                            [this _loadCommentMoreData];
                        }
                        
                        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"提示",nil),nil] message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil];
                        [alertview show];
                    }];
    
}

#pragma -mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    [UIView animateWithDuration:0.35f animations:^{
        self.commentBottomView.top = self.changeVIew.bottom - 2 - kbSize.height;
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.view.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.view.hidden = YES;
}


@end
